using System.Runtime.InteropServices;

namespace JamesZhao.Engine.Utils
{
    internal class SystemTime
    {
        private readonly double _countsPerMs;
        private readonly double _countsPerNs;

        public SystemTime()
        {
            long tempCountsPerSec;
            QueryPerformanceFrequency(out tempCountsPerSec);
            _countsPerMs = tempCountsPerSec/1000.0;
            _countsPerNs = _countsPerMs/1000.0;
        }

        public long CurrentTimeInTicks
        {
            get
            {
                long currentTime;
                QueryPerformanceCounter(out currentTime);
                return currentTime;
            }
        }

        public double CurrentTimeInMs
        {
            get { return CurrentTimeInTicks/_countsPerMs; }
        }

        public double CurrentTimeInNs
        {
            get { return CurrentTimeInTicks/_countsPerNs; }
        }

        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(
            out long lpPerformanceCount);

        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceFrequency(
            out long lpFrequency);
    }
}